package com.lz.yz.reward.web;

import com.github.pagehelper.PageHelper;
import com.lz.yz.base.entity.ReturnValue;
import com.lz.yz.outing.service.SyncLockService;
import com.lz.yz.reward.model.AnswerModel;
import com.lz.yz.reward.param.RewardParam;
import com.lz.yz.reward.service.AnswerService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;

/**
 * Created by CodeGenerator on 2017/11/14.
 */
@RestController
@RequestMapping("/answer")
public class AnswerController {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private AnswerService answerService;

    @Autowired
    SyncLockService syncLockService;

    @Autowired
    HttpServletRequest request;

    @ApiOperation(value = "回答悬赏", response = AnswerModel.class)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "Authorization", value = "Bearer token", defaultValue = "Bearer "),
            @ApiImplicitParam(name = "model", value = "回答实体类", dataType = "AnswerModel", paramType = "body"),
    })
    @PostMapping
    public ReturnValue add(@RequestBody AnswerModel model) {
        try {
            Integer accountId = (Integer) request.getAttribute("accountId");
            model.setAccountId(accountId);
            return answerService.add(model);
        } catch (Exception e) {
            logger.error("回答悬赏出错" + model, e);
            return ReturnValue.generateFalseReturnValue("回答悬赏失败");
        }
    }


    /**
     * 采纳回答
     *
     * @return
     */
    @PostMapping("adoption")
    public ReturnValue adoption(@RequestBody AnswerModel model) {
        try {
            if (syncLockService.syncLock("adoption/" + model.getRewardId() + "/" + model.getAnswerId(), 1, TimeUnit.SECONDS)) {
                return answerService.adoptionAnswer(model);
            } else {
                return ReturnValue.generateFalseReturnValue("请不要重复提交!");
            }
        } catch (Exception e) {
            logger.error("采纳回答失败", e);
            return ReturnValue.generateFalseReturnValue("采纳回答失败!");
        }
    }

    @ApiOperation(value = "大咖回答列表", response = AnswerModel.class)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "Authorization", value = "Bearer token", defaultValue = "Bearer "),
            @ApiImplicitParam(paramType = "query", name = "page", value = "当前页"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "每页条数"),
    })
    @GetMapping("my")
    public ReturnValue myAnswer(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "20") Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        Integer accountId = (Integer) request.getAttribute("accountId");
        try {
            RewardParam param = new RewardParam(page, pageSize);
            param.setLoginId(accountId);
            return answerService.myAnswer(param);
        } catch (Exception e) {
            logger.error("查询大咖回答列表出错" + accountId, e);
            return ReturnValue.generateFalseReturnValue("查询大咖回答列表失败");
        }
    }
}
